/*
  学习目标：避免组件无效更新 - shouldComponent决定是否更新

*/

import React from 'react';
export default class App extends React.Component {
  state = {
    count: 0,
    msg: '~',
  };

  handleClick = () => {
    this.setState({ count: this.state.count + 1 });
  };

  handleClickMsg = () => {
    this.setState({ msg: this.state.msg + '~' });
  };

  render() {
    return (
      <div>
        <h1>数量： {this.state.count}</h1>
        <h1>msg {this.state.msg}</h1>
        <button onClick={this.handleClick}>点我修改count</button>
        <button onClick={this.handleClickMsg}>点我修改msg</button>
        <Footer count={this.state.count}></Footer>
      </div>
    );
  }
}

class Footer extends React.Component {
  componentDidUpdate() {
    console.log('Footer被更新了  ----->  ');
  }

  shouldComponentUpdate(nextProps) {
    // 比较props.count 决定是否更新

    // 返回true表示更新
    if (nextProps.count !== this.props.count) return true;
    return false;
    // 返回false表示不更新
  }

  render() {
    return <div>我是Footer - {this.props.count}</div>;
  }
}
